﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class AsciiDecHex : Form
    {
        public AsciiDecHex()
        {
            InitializeComponent();
        }

        private void txt_Ascii_KeyUp(object sender, KeyEventArgs e)
        {
            txt_Bytes.Clear();
            txt_Decimal.Clear();

            Char[] asciiString = txt_Ascii.Text.ToCharArray();
            
            foreach (char aChar in asciiString)
            {
                //Get the ascii text and convert to DEC
                txt_Decimal.Text += Convert.ToInt32(aChar).ToString() + " ";
                
                //Get the ascii text and convert to bytes
                txt_Bytes.Text += BitConverter.ToString(new Byte[] {Convert.ToByte(aChar)},0) + " ";
            }

            //Clean up trailing spaces
            txt_Bytes.Text = txt_Bytes.Text.Trim();
            txt_Decimal.Text = txt_Decimal.Text.Trim();
        }

        private void txt_Bytes_KeyUp(object sender, KeyEventArgs e)
        {
            //Sample data
            //4d 65 20 74 6f 6f 2e 20 49 20 61 6d 20 68 65 61 64 69 6e 67 20 6f 75 74 20 74 6f 20 67 65 74 20 61 20 62 75 72 67 65 72 20 61 6e 64 20 61 20 63 6f 6c 64 20 6f 6e 20 72 69 67 68 74 20 6e 6f 77﻿
            txt_Ascii.Clear();
            txt_Decimal.Clear();

            try
            {
                byte[] hexArray = StringToByteArray(txt_Bytes.Text);

                foreach (byte aByte in hexArray)
                {
                    txt_Ascii.Text += Convert.ToChar(aByte);
                    txt_Decimal.Text += Convert.ToInt32(aByte).ToString() + " ";
                }

                //Clean up trailing spaces
                txt_Ascii.Text = txt_Ascii.Text.Trim();
                txt_Decimal.Text = txt_Decimal.Text.Trim();

                errorProvider1.Clear();
            }
            catch (Exception)
            {
                errorProvider1.SetError(lbl_Bytes, "Error converting bytes. Verify data is in the proper format (FF-00 or FF 00)");
            }
        }

        public static byte[] StringToByteArray(String hex)
        {
            hex = hex.Replace("-", "").Replace(" ", ""); //remove dashes and spaces
            int NumberChars = hex.Length / 2;
            byte[] bytes = new byte[NumberChars];
            System.IO.StringReader sr = new System.IO.StringReader(hex);
            for (int i = 0; i < NumberChars; i++)
                bytes[i] = Convert.ToByte(new string(new char[2] { (char)sr.Read(), (char)sr.Read() }), 16);
            sr.Dispose();
            return bytes;
        }

        private void txt_Decimal_KeyUp(object sender, KeyEventArgs e)
        {
            txt_Bytes.Clear();
            txt_Ascii.Clear();

            try{
                string[] decStringArray = txt_Decimal.Text.Trim().Split(' ');
                short aDec = -1;
                foreach (string aDecString in decStringArray)
                {
                    aDec = Int16.Parse(aDecString);
                
                    if (aDec != -1)
                    {
                        txt_Ascii.Text += Convert.ToChar(aDec);
                        txt_Bytes.Text += BitConverter.ToString(BitConverter.GetBytes(aDec), 0, 1) + " ";
                    }
                    errorProvider1.Clear();    
                }
            }
            catch(Exception err){
                errorProvider1.SetError(lbl_Decimal, "Error converting decimal data. Verify the data is in the proper format (123 122 0).");
            }

            //Clean up trailing spaces
            txt_Ascii.Text = txt_Ascii.Text.Trim();
            txt_Bytes.Text = txt_Bytes.Text.Trim();
        }

    }
}
